#!/usr/bin/perl
##
## preflight for LenovoUTIL.pkg.pkg
##
## Copyright (C) 2011 - 2012 Lenovo (Beijing) Limited
## Copyright (C) 2009 - 2012 Brother Industries, LTD.


##### Library ########################################################
use Foundation;
use File::Path;

##### Grabal ########################################################
my $EXIT_VALUE 		= 0;


##### Main ########################################################
my $PKG_LOCATION = $ARGV[0];
&preflightMain();
0;

##### Functions ########################################################
#
# Kill the specified process.
#	arg0:	Process id which you want to kill.
#	Return value:	Returns the number of processes successfully signaled.
#
sub killProcess
{
	#my $this_func_name = ( caller 0 )[3];
	my $retVal = 0;
	my $processID = $_[0];
	
	if( $processID )
	{
		$retVal = kill(SIGKILL, $processID);
	}
	
	return ${retVal};
}

#
# Terminate the specified process.
#	arg0:	Process id which you want to terminate.
#	Return value:	Returns the number of processes successfully signaled.
#
sub terminateProcess
{
	#my $this_func_name = ( caller 0 )[3];
	my $retVal = 0;
	my $processID = $_[0];
	
	if( $processID )
	{
		$retVal = kill(SIGTERM, $processID);
	}
	
	return ${retVal};
}

#
# Get the specified process id.
#	arg0:	The name of process which you want to get id.
#	Return value:	An array of process ID, otherwise -1 if the process not found.
#
sub getTargetProgramProcessID
{
	my $this_func_name = ( caller 0 )[3];
	my @retVal = ();
	my $processName = $_[0];
	
	if( $processName )
	{
		my $command = "ps -axcocommand,pid | grep \"".${processName}."\" |";
		
		open(PS,${command});
		@cmd = <PS>;
		close(PS);
		
		foreach $cmd (@cmd) 
		{
			my $pt = rindex($cmd, " ");
			if( $pt != -1 )
			{
				push(@retVal, substr($cmd, $pt+1));
			}
		} 
	}
	
	return @{retVal};
}




#
# Call the module which increments the program version.
#	arg0:	The path to the bundle program root.
#	Return value:	none.
#
sub quitTargetProgram
{
	my $this_func_name = ( caller 0 )[3];
	my @processes = 0;
	my $program = $_[0];
	
	@processes = &getTargetProgramProcessID($program);
	
	if( !(@processes[0]) )
	{
		print STDERR $this_func_name."(): Process is not found. ($program)\n";
	}
	else
	{
		foreach $process (@processes) 
		{
			if( &terminateProcess($process) == 1)
			{
				print STDERR $this_func_name."(): Process is terminated. ($program, $process)\n";
			}
			elsif( &killProcess($process) == 1)
			{
				print STDERR $this_func_name."(): Process is killed. ($program, $process)\n";
			}
			else
			{
				print STDERR $this_func_name."(): Could not quit. ($program, $process)\n";
			}			
		} 
	}	
}

#
# Remove file/directory.
#	arg0:	The path to the file/directory.
#	Return value:	none.
#
sub remove
{
	my $this_func_name = ( caller 0 )[3];
	my $retVal = 0;
	my $target = $_[0];
	
	if( -e "$target" )
	{
		system("sudo rm -rf $target");
	}
	else
	{
		print STDERR $this_func_name."(): No such file or directory. ($target)\n";
	}	
	
}

#
# Main
#
sub preflightMain
{
	#my $this_func_name = ( caller 0 )[3];
	#my $retVal = 0;

	# quit process
	&quitTargetProgram("InstallUtility");

	# remove old startup system registration
	my $loginItemExe = $PKG_LOCATION."/Contents/Resources/LoginItem";
	if( -e "$loginItemExe" )
	{
		system("'$loginItemExe' global remove /Library/Printers/Lenovo/Utilities/InstallUtility.app/Contents/Resources/InstUtilLaunch.app");
	}

	# remove old preferences files
	&remove("/Library/Printers/Lenovo/Utilities/InstallUtility.app");
	&remove("/Library/Preferences/Lenovo/com.lenovo.instmodel.plist");
	
	#return $retVal;
}

